


import {
  get,
  post,
  put,
} from "./base_service"


/**
 * 佳宝订单相关接口
 *
 * allen  2023-12-19
 */

/**
 * 
 * 获取订单列表
 * type 0 待付款 1 待发货 2 待收货 3 待评价 4 已完成  -3 售后/退款
 */

const queryOrderList = (type, page, limit = 10) => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/list?type=" + type + "&page=" + page + "&limit=" + limit).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 
 * 取消订单
 * {id:1} 参数要传在form-data上
 */

const cancelOrder = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/cancel", data, false, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 
 * 提现申请
 * {id:1} 参数要传在form-data上
 */

const walletCashApply = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/extract/cash", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 
 * 删除订单
 * {id:1} 参数要传在form-data上
 */

const delOrder = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/del", data, false, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 
 * 当前订单顶部信息
 */

const orderDataInfo = () => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/data").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 
 * 在订单页的立即支付,在结算页的立即支付也是这个
 * {
 * "orderNo":"order68544170369182401348461",
 * "payChannel":"weixinh5",
 * "payType":"weixin",
 * "scene":0
 * }
 */

const orderPay = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/pay/payment", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 
 * 查看订单详情
 * order685441703691824013484
 */

const queryOrderDetail = (order) => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/detail/" + order).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 从购物车过去的哈
 * 立即结算页面生成预订单
 * data:{
 *   orderDetails:[{shoppingCartId:6},{shoppingCartId:7}],
 *   preOrderType:'shoppingCart'
 * }
 */

const createPreOrder = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/pre/order", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 通过预订单生成的订单信息
 * preOrderNo
 */

const createPreOrderInfo = (preOrderNo) => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/load/pre/" + preOrderNo).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 *点击立即结算，生成一个正式订单
 */

const createOrder = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/create", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 *获取对应类型订单的数量
 */

const getOrderCategoryNums = () => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/data").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 *订单价格变化确认
 */

const confirmOrderPriceComputed = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/computed/price", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 *订单确认收货
 */

const confirmOrderReceive = (id) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/take?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 *获取退款订单信息
 */

const queryReturnOrderInfo = (orderId) => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/apply/refund/" + orderId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 *查询退款原因供客户选择后台可配
 */

const queryReturnTexts = () => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/refund/reason").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 *提交退款申请
 */

const submitRefundOrderMoney = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/front/order/refund", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}



/**
 *查看订单物流信息
 */

const seeDeliveryInfo = (orderNo) => {
  return new Promise((resolve, reject) => {
    get("/api/front/order/express/" + orderNo).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 *拒绝退款
 */

const refundOaOrder = (orderNo, reason) => {
  return new Promise((resolve, reject) => {
    get("/api/front/oa/order/refund/refuse?orderNo=" + orderNo + '&reason=' + reason).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 *同意退款
 */

const agreeOaOrderRefund = (orderNo, amount) => {
  return new Promise((resolve, reject) => {
    let money = "";
    if (amount != 0 && amount != '') {
      money = amount
    }
    get("/api/front/oa/order/refund?orderNo=" + orderNo + '&amount=' + money).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.message)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}









module.exports = {
  queryOrderList,
  cancelOrder,//取消订单
  orderDataInfo,//订单顶部展示信息
  orderPay,//在订单页立即支付
  queryOrderDetail,//查看订单详情
  createPreOrder, //生成预订单
  createPreOrderInfo,//通过预订单生成对应订单信息
  createOrder,//  点击立即结算，生成一个正式订单
  getOrderCategoryNums,//获取对应订单类型的数量
  delOrder,//删除一个订单
  confirmOrderPriceComputed,//订单价格变化确认
  confirmOrderReceive,
  queryReturnOrderInfo,
  queryReturnTexts,
  submitRefundOrderMoney,
  seeDeliveryInfo,//查看物流信息
  refundOaOrder,//拒绝退款
  agreeOaOrderRefund,//同意退款
  walletCashApply,//申请提现
}

